package com.pmsystem.service;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.pmsystem.dao.AdministratorMapper;
import com.pmsystem.dao.UserMapper;
import com.pmsystem.pojo.Administrator;
import com.pmsystem.pojo.AdministratorExample;
import com.pmsystem.pojo.AdministratorExample.Criteria;
import com.pmsystem.pojo.User;
import com.pmsystem.util.BeanUtil;
import com.pmsystem.util.MD5;


@Service
public class AdministratorService {

	
	@Autowired
	private AdministratorMapper administratorMapper;
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 
	 * <p>Title: findAdmin</p >  
	 * <p>Description: 按一定条件查找Admin信息，注意SQL</p >  
	 * @author 泽强
	 * @param map 
	 * @return
	 */
	public List<Administrator> findAdmin(Map<String, Object> map) {
		Integer pageNo = (Integer) map.get("pageNo");
		Integer pageSize = (Integer) map.get("pageSize");
		if(pageNo!=null&&pageSize!=null) {
			int start = (pageNo - 1) * pageSize;
			map.put("start", start);
		} 
		return administratorMapper.findAdmin(BeanUtil.mapToMapWithoutNull(map));
	}

	
	/**
	 * 
	 * <p>Title: addAdministrator</p>
	 * <p>Description:  增加二级管理员</p> 
	 * @author 泽强
	 * @param administrator
	 * @return
	 */
	
	public int addAdministrator(Administrator administrator) {
		String encodePwd = MD5.MD5Eencryption(administrator.getAdminPassword(), administrator.getAdminAcc());
		if(encodePwd==null)
			return 0;
		administrator.setAdminPassword(encodePwd);
		return administratorMapper.insertSelective(administrator);
	}

	/**
	 * 
	 * <p>Title: deleteAdministrator</p>
	 * <p>Description: 根据id批量删除二级管理员 </p> 
	 * @author 泽强
	 * @param idList
	 */

//	public void deleteAdministrator(List<String> idList) {
//		
//		AdministratorExample example = new AdministratorExample();
//		Criteria criteria =  example .createCriteria();
//		criteria.andAdminaccIn(idList);
//		administratorMapper.deleteByExample(example);
//		
//	}

	/**
	 * 
	 * <p>Title: updateAdministrator</p>
	 * <p>Description: 更新二级管理员信息 </p> 
	 * @author 泽强
	 * @param administrator
	 */
	public boolean updateAdministrator(Administrator administrator) {
		
		int flag = administratorMapper.updateByPrimaryKeySelective(administrator);
		if(flag==0) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * <p>Title: login</p >  
	 * <p>Description: 二级管理员登陆</p >  
	 * @author 泽强
	 * @param username
	 * @param password
	 * @return
	 */
	public Administrator login(String username, String password) {
		String encodePwd = MD5.MD5Eencryption(password, username);
		if(encodePwd==null)
			return null;
		return administratorMapper.login(username,encodePwd);
	}


	/**  
	 * <p>Title: findUserUncheck</p >  
	 * <p>Description:查询所有待审核用户信息</p >  
	 * @author 魏家洛
	 * @return  
	 */  
	public List<User> findUserUncheck() {
		return userMapper.selectUserEableEq2();
	}


	/**  
	 * <p>Title: passUser</p >  
	 * <p>Description:通过业主注册账号的审核</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return  
	 */  
	public boolean passUser(String userId) {
		return userMapper.updateUserBeUse(userId)==1;
	}


	/**  
	 * <p>Title: deleteUser</p >  
	 * <p>Description:删除用户或不通过业主账号注册申请</p >  
	 * @author 魏家洛
	 * @param userId
	 * @return  
	 */  
	public boolean deleteUser(String userId) {
		return userMapper.deleteUser(userId)==1;
	}
}
